﻿using AutoMapper;
using HongYeSystemWebApi.Templet.Entity.Entities;
using HongYeSystemWebApi.Templet.Entity.Request;
using HongYeSystemWebApi.Templet.Entity.Request.SysCompanyEntity;
using HongYeSystemWebApi.Templet.Entity.Request.SysProject;
using HongYeSystemWebApi.Templet.Entity.Response;
using HongYeSystemWebApi.Templet.Entity.Response.Project;
using HongYeSystemWebApi.Templet.Entity.Response.SysBankinfo;
using HongYeSystemWebApi.Templet.Entity.Response.SysCompanyEntity;
using HongYeSystemWebApi.Templet.IService;
using SqlSugar;
using System;
using System.Collections.Generic;
using System.Threading.Tasks;

namespace HongYeSystemWebApi.Templet.Service
{
    public class CompanyEntityService : ICompanyEntityService
    {

        private readonly SqlSugarClient _db;
        private readonly IMapper _mapper;
        /// <summary>
        /// 构造函数依赖注入
        /// </summary>
        /// <param name="db"></param>
        public CompanyEntityService(IHelpDB db, IMapper mapper)
        {
            this._db = db.OpenDB();
            _mapper = mapper;
        }
        public async Task DeleteAsync(MultiIdRequest request)
        {
            List<CompanyEntity> DelData = new List<CompanyEntity>();
            foreach (var item in request.Ids)
            {
                var project = await _db.Queryable<CompanyEntity>().FirstAsync(a => a.Id == item);
                project.Delete = true;
                project.OperatDate = DateTime.Now;
                DelData.Add(project);
            }
            await _db.Updateable(DelData).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        } 
        public async Task InsertAsync(SysCompanyEntityInsertRequest request)
        {
            var data = _mapper.Map<CompanyEntity>(request);
            data.OperatDate = DateTime.Now;
              await _db.Insertable(data).ExecuteReturnIdentityAsync();
            //添加到供应商
            var id = await _db.Insertable(new Supplier() { Name = request.CompanyName, OperatDate = DateTime.Now }).ExecuteReturnIdentityAsync();
            await _db.Insertable(new ProjectEntity() { ProjectName = request.CompanyName+"内部开销", CreateDate=DateTime.Now, PartyA= id, ProjectNumber= request.CompanyName+"001", Delete=false, OperatDate=DateTime.Now }).ExecuteCommandAsync();

        }
        public async Task UpdateAsync(SysCompanyEntityUpdataRequest request)
        {
            var datafir = await _db.Queryable<CompanyEntity>().FirstAsync(X => X.Id == request.Id);
            if (datafir == null)
            {
                throw new Exception("未找到修改原始数据");
            }
            var data = _mapper.Map<CompanyEntity>(request);
            data.OperatDate = DateTime.Now;
            await _db.Updateable(data).WhereColumns(it => new { it.Id }).ExecuteCommandAsync();
        }

        public async Task<PageModel<SysCompanyDataPageResponse>> GetListAsync(KeyPageRequest request)
        {
            var data = _db.Queryable<CompanyEntity>().Where(a => !a.Delete);
            if (!string.IsNullOrEmpty(request.key))
            {
                data = data.Where(x => x.CompanyName.Contains(request.key));
            }
            RefAsync<int> Total = 0;
            var resultpage = await data.OrderBy(x => x.Id, OrderByType.Desc).ToPageListAsync(request.PageNumber, request.PageSize, Total);
            var result = _mapper.Map<List<SysCompanyDataPageResponse>>(resultpage);
         
            return new PageModel<SysCompanyDataPageResponse>() { Data = result, Total = Total };
        }
    }
}
